#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import re
import qqip
import sys

curdir=sys.path[0]

def ischineseip(host):
	return ('市' in host[0] or '省' in host[0] or '台湾' in host[0] or '澳门' in host[0] or  '香港' in host[0] or  '中国' in host[0] or '大学' in host[0]) and not ('雅虎' in host[0] or '雅虎' in host[1] or  '蜘蛛' in host[1] or '百度' in host[1] or '腾讯' in host[1])


province=['辽宁','江苏','湖北','四川','陕西','广东','河北','山西','河南','吉林','黑龙江','山东','安徽','浙江','福建','湖南','江西','贵州','云南','甘肃','青海','海南','台湾', '内蒙', '宁夏', '广西','西藏','新疆', '北京', '天津','上海','重庆','香港','澳门']

procount=[]

for i in province:
	procount.append([i,[0,0]])

procount=dict(procount)

def IsInt( str ):
	""" Is the given string an integer?	"""
	ok = 1
	try:
		num = int(str)
	except ValueError:
		ok = 0
	return ok

delays=[]
delays_withip=[]

totalip=0
iptable=[]
iplist=[]

print 'Parsing files under data directory'
for i in os.listdir('%s/data'%curdir):
	f=open('%s/data/%s' % (curdir,i))
	g=f.readlines()
	f.close()
	finaltime=0
	finalip=''
	finalhop=0
	for j in g:
		t=re.findall('\d+\.\d+ ms', j)
		ip=re.findall('\d+\.\d+\.\d+\.\d+',j)
		hop=j.strip().split()
		if IsInt(hop[0]):
			hop=int(hop[0])
		time=0
		for k in t:
			time=time+float(k.strip('ms').strip(' '))
		if len(t)>0:
			time=time/len(t)
		if time > 0:
			finaltime=time
			finalip=ip
			finalhop=hop
			for l in ip:
				iptable.append([hop,l,time])
				iplist.append([l,[]])

ipdict=dict(iplist)

for i in iptable:
	ipdict[i[1]].append([i[0],i[2]])	

for i in ipdict.keys():
	l=ipdict[i]
	ldict=[]
	for j in l:
		ldict.append([j[0],[0,0]])
	ldict=dict(ldict)
	for j in l:
		ldict[j[0]][0]=ldict[j[0]][0]+1
		ldict[j[0]][1]=ldict[j[0]][1]+j[1]
	for j in ldict.keys():
		ldict[j]=[ldict[j][0], ldict[j][1]/ldict[j][0]]
	ipdict[i]=ldict

ip_locater = qqip.IpLocater(r'%s/qqwry.db'% curdir)

gatewayip=[]

for i in range(1,15):
	print "hop=", i
	hopt=[]
	for j in ipdict.keys():
		if ipdict[j].has_key(i):
			if int(round(ipdict[j][i][1]))>100:
				hopt.append([int(round(ipdict[j][i][1])), j, ipdict[j][i][0]]) 
			'''
			address = ip_locater.getIpAddr(qqip.string2ip(j))
			host=address.decode('gb18030').encode('utf8')
			host=host.split('/')
			host[0]=host[0].strip()
			host[0]=host[0].strip('CZ88.NET')
			host[1]=host[1].strip()
			host[1]=host[1].strip('CZ88.NET')
			if ischineseip(host):
			'''

	hopt.sort()

	realhop=[]
	for ii in os.listdir('%s/data'%curdir):
		f=open('%s/data/%s' % (curdir,ii))
		g=f.readlines()
		f.close()
		ingate=0
		inhop=0
		count =0
		for j in g:
			count =count+1	
			if count == 1:
				continue
			ip=re.findall('\d+\.\d+\.\d+\.\d+',j)
			for k in ip:
				if k in gatewayip:
					ingate=1
					break
			if ingate==1:
				break

			for k in hopt:
				if k[1] in ip and not (k[1] in gatewayip):
					inhop=1 
					realhop.append(k[1])
					gatewayip.append(k[1])
					break
			if inhop==1:
				break
	
		realhop=list(set(realhop))
	for j in realhop:
		address = ip_locater.getIpAddr(qqip.string2ip(j))
		host=address.decode('gb18030').encode('utf8')
		host=host.split('/')
		host[0]=host[0].strip()
		host[0]=host[0].strip('CZ88.NET')
		host[1]=host[1].strip()
		host[1]=host[1].strip('CZ88.NET')
		if ischineseip(host):
			print '%16s (%4d,%3d)  %s | %s' % (j, ipdict[j][i][0], int(round(ipdict[j][i][1])), host[0], host[1])

